﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SQLite;

namespace KeyboardImageViewer
{
    public class SummaryTask : IProcessTask
    {
        private long imageId;
        private string filePath;
        private DateTime refreshDate;
        private bool newRow;
        private ImageSummary summary;

        public SummaryTask(long imageId, string filePath, DateTime refreshDate, bool newRow)
        {
            this.imageId = imageId;
            this.filePath = filePath;
            this.refreshDate = refreshDate;
            this.newRow = newRow;
        }

        public void Process()
        {
            this.summary = DuplicateUtils.GetImageSummary(this.filePath);
        }

        public void Commit(SQLiteConnection connection)
        {
            if (this.newRow)
            {
                SQLiteCommand insertCommand = new SQLiteCommand(connection);
                insertCommand.CommandText = "INSERT INTO summaries " +
                    "(imageId, ineligible, lastRefreshDate, thumbnail, averageVariance, fractionDrasticPixelBorders, hashPrefix) VALUES " +
                    "(" + this.imageId + ", @ineligible, @lastRefreshDate, @thumbnail, " + this.summary.AverageVariance + ", " + this.summary.FractionDrasticPixelBorders + ", '" + this.summary.HashPrefix + "')";
                var ineligibleParam = insertCommand.Parameters.Add("ineligible", System.Data.DbType.Boolean);
                var refreshDateParam = insertCommand.Parameters.Add("lastRefreshDate", System.Data.DbType.DateTime);
                var thumbnailParam = insertCommand.Parameters.Add("thumbnail", System.Data.DbType.Binary);

                ineligibleParam.Value = this.summary.Ineligible;
                refreshDateParam.Value = this.refreshDate;
                thumbnailParam.Value = this.summary.Thumbnail;

                insertCommand.ExecuteNonQuery();
            }
            else
            {
                SQLiteCommand updateCommand = new SQLiteCommand(connection);
                updateCommand.CommandText = "UPDATE summaries SET " +
                    "ineligible = @ineligible, " +
                    "lastRefreshDate = @lastRefreshDate, " +
                    "thumbnail = @thumbnail, " +
                    "averageVariance = " + this.summary.AverageVariance + ", " +
                    "fractionDrasticPixelBorders = " + this.summary.FractionDrasticPixelBorders + ", " +
                    "hashPrefix = '" + this.summary.HashPrefix + "' WHERE imageId = " + this.imageId;
                var ineligibleParam = updateCommand.Parameters.Add("ineligible", System.Data.DbType.Boolean);
                var refreshDateParam = updateCommand.Parameters.Add("lastRefreshDate", System.Data.DbType.DateTime);
                var thumbnailParam = updateCommand.Parameters.Add("thumbnail", System.Data.DbType.Binary);

                ineligibleParam.Value = this.summary.Ineligible;
                refreshDateParam.Value = this.refreshDate;
                thumbnailParam.Value = this.summary.Thumbnail;

                updateCommand.ExecuteNonQuery();
            }
        }
    }
}
